package fanfou4j.type;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import fanfou4j.org.json.JSONArray;
import fanfou4j.org.json.JSONObject;
import fanfou4j.util.DateUtil;

public class Message {
	private String id;
	private String text;
	private String sender_id;
	private String recipient_id;
	private Date created_at;
	private String sender_screen_name;
	private String recipient_screen_name;
	private User sender;
	private User recipient;

	public Message(JSONObject json) throws Exception {
		init(json);
	}
	private void init(JSONObject json) throws Exception {
		if (json != null) {
			id=json.getString("id");
			text=json.getString("text");
			sender_id=json.getString("sender_id");
			recipient_id=json.getString("recipient_id");
			created_at = DateUtil.parseDate(json.getString("created_at"),"EEE MMM dd HH:mm:ss ZZZZZ yyyy");
			sender_screen_name=json.getString("sender_screen_name");
			recipient_screen_name=json.getString("recipient_screen_name");
			if(!json.isNull("sender")){
				sender=new User(json.getJSONObject("sender"));			
			}
			if(!json.isNull("recipient")){
				recipient=new User(json.getJSONObject("recipient"));			
			}
		}
	}
	public static List<Message> getMessageList(JSONArray array) throws Exception{		
		int size=array.length();
		List<Message> list=new ArrayList<Message>();
		for (int i = 0; i < size; i++) {
			list.add(new Message(array.getJSONObject(i)));
		}
		return list;
	}
	public String getId() {
		return id;
	}
	public String getText() {
		return text;
	}
	public String getSender_id() {
		return sender_id;
	}
	public String getRecipient_id() {
		return recipient_id;
	}
	public Date getCreated_at() {
		return created_at;
	}
	public String getSender_screen_name() {
		return sender_screen_name;
	}
	public String getRecipient_screen_name() {
		return recipient_screen_name;
	}
	public User getSender() {
		return sender;
	}
	public User getRecipient() {
		return recipient;
	}
	@Override
	public String toString() {
		return "Message [id=" + id + ", text=" + text + ", sender_id="
				+ sender_id + ", recipient_id=" + recipient_id
				+ ", created_at=" + created_at + ", sender_screen_name="
				+ sender_screen_name + ", recipient_screen_name="
				+ recipient_screen_name + ", sender=" + sender + ", recipient="
				+ recipient + "]";
	}
	

}
